/**
 * 请求工具类
 */
import Request from 'luch-request/src/lib/luch-request.js'
import reqConfig from '@/common/config/reqConfig.js'
import router from '@/router'
import store from "@/store"
const getTokenStorage = () => {
  let token = ''
  try {
    token =  store.state.hh_token
	console.log(token)
  } catch (e) {
    //TODO handle the exception
  }
  return token
}
const jhrsHttp = new Request()
jhrsHttp.setConfig((config) => { /* 设置全局配置 */
  config.baseURL = reqConfig.jhrsUrl
  return config
})


jhrsHttp.interceptors.request.use((config) => { /* 请求之前拦截器。可以使用async await 做异步操作 */
  config.header = {
    ...config.header,
    accessToken: getTokenStorage()
  }
  /*
 if (!token) { // 如果token不存在，return Promise.reject(config) 会取消本次请求
   return Promise.reject(config)
 }
 */
  return config
}, (config) => {
  return Promise.reject(config)
})

// 必须使用异步函数，注意
jhrsHttp.interceptors.response.use(async (response) => { /* 请求之后拦截器。可以使用async await 做异步操作  */
	let code =null
	let data ={}
	console.log(response)
	let statusCode = response.statusCode
	if(statusCode){
		if(statusCode==200){
			code=response.data.code
			data=response.data
		}else{
			uni.showToast({
			title: response.errMsg || '系统异常',
			icon: 'none',
			duration:2000
			});
			return Promise.reject(response)
		}
	 }else{
		uni.showToast({
	 		title: response.errMsg || '系统异常',
	 		icon: 'none',
	 		duration:2000
	 	});
	 	return Promise.reject(response)
	 }
	
	if (code!=null) {
		if(code==0||code ==200){
			  return data
		}else if (code < 0) {
  		  	uni.showToast({
  		  		title: data.message,
  		  		icon: 'none',
  		  		duration:2000
  		  	});
  		  	return Promise.reject(data)
		}else if (code > 0) {
  			if (code === 401) {
				console.log(code)
				store.commit("$uStore", {name:'hh_token',value:''})
				uni.navigateTo({
					url: '/pages/system/login/login?goBackFlag='+'true'
				})
				return Promise.resolve()
  			}else{
  				return Promise.reject(data)
  			}
  		}
  	}else {
		if (response.status) {
        if (response.status === 403) {
      	  uni.showToast({
      	  	title: 'Forbidden:'+response.message,
      	  	icon: 'none',
      	  	duration:2000
      	  });
          return Promise.reject(response)
        }
        if (response.status === 500) {
      	  uni.showToast({
      	  	title: '请求服务器异常,请稍后再试',
      	  	icon: 'none',
      	  	duration:2000
      	  });
          return Promise.reject(response)
        }
        if (response.status === 404) {
      	  uni.showToast({
      	  	title: '服务端未提供接口'+response.config.url,
      	  	icon: 'none',
      	  	duration:2000
      	  });
          return Promise.reject(response)
        }
      }else{
      	  
      	uni.showToast({
      	    title: '系统异常：'+response.errMsg,
      	    icon: 'none',
      	    duration:2000
      	});
      	return Promise.reject(response)
      }
	}
}, (response) => { // 请求错误做点什么。可以使用async await 做异步操作
  uni.showToast({
  	title: '服务端异常'+response.errMsg+'--'+response.config.url,
  	icon: 'none',
  	duration:2000
  });
  return Promise.reject(response)
})

export {
  jhrsHttp
}
